package com.lyumall.gateway.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.reactive.CorsConfigurationSource;
import org.springframework.web.cors.reactive.CorsWebFilter;
import org.springframework.web.cors.reactive.UrlBasedCorsConfigurationSource;

@Configuration
public class CorsConfig {

    @Bean
    public CorsWebFilter corsWebFilter() {
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        CorsConfiguration corsConfiguration = new CorsConfiguration();
        // 允许请求头
        corsConfiguration.addAllowedHeader("*");
        // 允许请求方法
        corsConfiguration.addAllowedMethod("*");
        // 允许请求来源
        corsConfiguration.addAllowedOrigin("*");
        // 是否允许携带Cookie进行跨域
        corsConfiguration.setAllowCredentials(true);
        // 注册配置，任意路径都要注册配置
        source.registerCorsConfiguration("/**",corsConfiguration);
        return new CorsWebFilter(source);
    }

}
